<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 23, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

define ( "BROWSER_IE", "Internet Explorer" );
define ( "BROWSER_FIREFOX", "Firefox" );
define ( "BROWSER_NETSCAPE", "Netscape" );
define ( "BROWSER_SAFARI", "Safari" );
define ( "BROWSER_GALEON", "Galeon" );
define ( "BROWSER_GECKO_BASED", "Gecko based" );
define ( "BROWSER_KONQUEROR", "Konqueror" );
define ( "BROWSER_OPERA", "Opera" );
define ( "BROWSER_LYNX", "Lynx" );

RLibImporter::cls( "browser.Browser_Detection" );

class BrowserInfo extends Browser_Detection {
	var $name;
	var $version;
	var $operationSystem;
	var $isGecko = false;
	
	function BrowserInfo($useragent = null) {
		if (! (isset ( $useragent ) && strlen ( $useragent ))) {
			if (! isset ( $_SERVER ['HTTP_USER_AGENT'] )) {
				throw new Exception ( 'Environment variable HTTP_USER_AGENT is missing!' );
			}
			$useragent = $_SERVER ['HTTP_USER_AGENT'];
		}
		$this->operationSystem = $this->get_os ( $useragent );
		$this->_parse ( $useragent );
	}
	
	/**
	 * Parse infomation
	 *
	 * @param string $useragent
	 */
	function _parse($useragent) {
		//check for most popular browsers first
		//unfortunately that's ie. We also ignore opera and netscape 8 
		//because they sometimes send msie agent
		if (strpos ( $useragent, "MSIE" ) !== false && strpos ( $useragent, "Opera" ) === false && strpos ( $useragent, "Netscape" ) === false) {
			//deal with IE
			$found = preg_match ( "/MSIE ([0-9]{1}\.[0-9]{1,2})/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_IE;
			}
		} elseif (strpos ( $useragent, "Gecko" )) {
			$this->isGecko = true;
			//deal with Gecko based
			

			//if firefox
			$found = preg_match ( "/Firefox\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_FIREFOX;
			}
			
			//if Netscape (based on gecko)
			$found = preg_match ( "/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_NETSCAPE;
			}
			
			//if Safari (based on gecko)
			$found = preg_match ( "/Safari\/([0-9]{2,3}(\.[0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_SAFARI;
			}
			
			//if Galeon (based on gecko)
			$found = preg_match ( "/Galeon\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_GALEON;
			}
			
			//if Konqueror (based on gecko)
			$found = preg_match ( "/Konqueror\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_KONQUEROR;
			}
			
			//no specific Gecko found
			//return generic Gecko
			$this->version = $mathes [1];
			$this->name = BROWSER_GECKO_BASED;
		} 

		elseif (strpos ( $useragent, "Opera" ) !== false) {
			//deal with Opera
			$found = preg_match ( "/Opera[\/ ]([0-9]{1}\.[0-9]{1}([0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_OPERA;
			}
		} elseif (strpos ( $useragent, "Lynx" ) !== false) {
			//deal with Lynx			
			$found = preg_match ( "/Lynx\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_LYNX;
			}
		
		} elseif (strpos ( $useragent, "Netscape" ) !== false) {
			//NN8 with IE string
			$found = preg_match ( "/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes );
			if ($found) {
				$this->version = $mathes [1];
				$this->name = BROWSER_NETSCAPE;
			}
		} else {
			//unrecognized, this should be less than 1% of browsers (not counting bots like google etc)!
			return false;
		}
	}
}
?>